import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'newFundTrade',

  state: {},

  effects: {
    // 获取持仓信息
    *getFundInfo({ payload, callback }, { call, put }) {
      const res = yield call(api.fundTrade.getFundInfo, payload);
      if (res && res.code == '0') {
        callback(res?.data?.dataSource);
      } else {
        callback();
        message.error(res?.msg || '基本信息查询失败！');
      }
    },

    // 指令下达
    *SaveInstruction({ payload, callback }, { call, put }) {
      const res = yield call(api.fundTrade.SaveInstruction, payload);
      if (res && res.success == true) {
        message.success('指令下达成功');
        callback(res);
      } else {
        message.error(res.errorMsg);
      }
    },
    // 保存簿记
    *SaveInstructionw2({ payload, callback }, { call, put }) {
      const res = yield call(api.fundTrade.SaveInstructionw2, payload);
      if (res && res.success == true) {
        message.success('操作成功');
        callback(res);
      } else {
        message.error(res.errorMsg);
      }
    },
    // 存续管理
    *tradeBookSave({ payload, callback }, { call, put }) {
      const res = yield call(api.fundTrade.tradeBookSave, payload);
      if (res && res.success == true) {
        message.success('操作成功');
        callback(res);
      } else {
        message.error(res.errorMsg);
      }
    },
    // 交收簿记
    *getTradeInstruction({ payload, callback }, { call, put }) {
      const res = yield call(api.fundTrade.getTradeInstruction, payload);
      if (res && res.success == true) {
        message.success('交收成功');
        callback(res);
      } else {
        message.error(res.errorMsg);
      }
    },
    // 编辑簿记
    *confirmEdit({ payload, callback }, { call, put }) {
      const res = yield call(api.fundTrade.confirmEdit, payload);
      callback(res);
    },

    // 获取销售机构代码数据
    *getSalesOrganizationCodeData({ payload, callback }, { call, put }) {
      const res = yield call(api.fundTrade.getSalesOrganizationCodeData, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res?.message || '获取销售机构代码失败');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
